package cn.com.cfae.iras.doc.analyze.components.iaad;

import cn.com.cfae.iras.doc.analyze.beans.iaad.IaadIndicatorGradeDetail;
import cn.com.cfae.iras.doc.analyze.beans.iaad.IndustryIndicatorList;
import cn.com.cfae.iras.doc.analyze.beans.iaad.IndustryIndicatorThreshold;
import cn.com.cfae.iras.doc.analyze.beans.web.ContentAndPosition;
import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.components.integrity.I1001;
import cn.com.cfae.iras.doc.analyze.config.ApplicationContextHelper;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import com.google.gson.GsonBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 佟媛媛 on 18/11/7.
 * 水泥
 */

@Component("SN07")
@Scope("prototype")
public class SN07  extends BaseComponent {

    private static Logger log = LoggerFactory.getLogger(SN07.class);

    public List<IaadIndicatorGradeDetail> handlerIAAD(){
        List<IaadIndicatorGradeDetail> list = new ArrayList<>();
        List<IaadIndicatorGradeDetail> SLCN = getSLCN();//熟料产能（吨）
        if(CollectionUtils.isNotEmpty(SLCN)) list.addAll(SLCN);
        List<IaadIndicatorGradeDetail> XKGMJ = getXKGMJ();//房地产新开工面积
        if(CollectionUtils.isNotEmpty(XKGMJ)) list.addAll(XKGMJ);
        IaadIndicatorGradeDetail ZCGM = getZCGM();//资产规模
        if(ZCGM!=null) list.add(ZCGM);
        List<IaadIndicatorGradeDetail> CNLYL = getCNLYL();//产能利用率
        if(CollectionUtils.isNotEmpty(CNLYL)) list.addAll(CNLYL);
        IaadIndicatorGradeDetail YYSR = getYYSR();//营业收入增长率
        if(YYSR!=null) list.add(YYSR);
        IaadIndicatorGradeDetail EBITDA = getEBITDA();//EBITDA总资产报酬率
        if(EBITDA!=null) list.add(EBITDA);
        IaadIndicatorGradeDetail SJYJ = getSJYJ();//是否出具非标准审计意见
        if(SJYJ!=null) list.add(SJYJ);
        IaadIndicatorGradeDetail MLL = getMLL();//毛利率
        if(MLL!=null) list.add(MLL);
        IaadIndicatorGradeDetail JLR_YYSR = getJLR_YYSR();//净利率
        if(JLR_YYSR!=null) list.add(JLR_YYSR);
        IaadIndicatorGradeDetail ZFZ_ZZC = getZFZ_ZZC();//资产负债率
        if(ZFZ_ZZC!=null) list.add(ZFZ_ZZC);
        IaadIndicatorGradeDetail LDFZ_ZFZ = getLDFZ_ZFZ();//流动负债/负债合计
        if(LDFZ_ZFZ!=null) list.add(LDFZ_ZFZ);
        IaadIndicatorGradeDetail XJL = getXJL();//经营性净现金流净额
        if(XJL!=null) list.add(XJL);
        IaadIndicatorGradeDetail LDZC_LDFZ = getLDZC_LDFZ();//流动比率（短期偿债指标）
        if(LDZC_LDFZ!=null) list.add(LDZC_LDFZ);
        IaadIndicatorGradeDetail ALL_EBITDA = getALL_EBITDA();//全部债务/EBITDA（长期偿债指标）
        if(ALL_EBITDA!=null) list.add(ALL_EBITDA);
        IaadIndicatorGradeDetail SDXJ = getSDXJ();//销售商品、提供劳务收到的现金/短期负债
        if(SDXJ!=null) list.add(SDXJ);
        IaadIndicatorGradeDetail HBZJ = getHBZJ();//货币资金/短期债务
        if(HBZJ!=null) list.add(HBZJ);
        log.info("水泥行业指标：{}",new GsonBuilder().create().toJson(list));
        return list;
    }

    //熟料产能（吨）
    /**
     1、熟料产能（吨） --》SLCN
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、抽取多个值，人工选择确认；
     3、缺失指标分类；"
     */
    private List<IaadIndicatorGradeDetail> getSLCN(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("SLCN");
            List<IaadIndicatorGradeDetail> list = new ArrayList<>();
            List<KVText> kvTextList = getKVTextValues("M-5","SN","SLCN");
            int pageNum = getChapterModel("M-5").getStartPage();
            if(CollectionUtils.isNotEmpty(kvTextList)){
                for(KVText kvText : kvTextList){
                    String value = kvText.getText();
                    double v = getDouble(value);
                    List<ContentAndPosition> textPositionList = getContentAndPositionList2(kvText);
                    if(StringUtils.isEmpty(value)){
                        continue;
                    }
                    IaadIndicatorGradeDetail iaadIndicatorGradeDetail = getIaadIndicatorGradeDetail(indicator,textPositionList,doubleToBigDecimal(v),pageNum,value,String.format("熟料产能%s",value));
                    if(iaadIndicatorGradeDetail!=null) list.add(iaadIndicatorGradeDetail);
                }
            }
            return list;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //房地产新开工面积
    /**
     2、房地产新开工面积 --》XKGMJ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、缺失指标分类？"
     */
    private List<IaadIndicatorGradeDetail> getXKGMJ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("FDCXKGMJ");
            List<IaadIndicatorGradeDetail> list = new ArrayList<>();
            List<KVText> kvTextList = getKVTextValues("M-5","SN","XKGMJ");
            int pageNum = getChapterModel("M-5").getStartPage();
            if(CollectionUtils.isNotEmpty(kvTextList)){
                for(KVText kvText : kvTextList){
                    String value = kvText.getText();
                    double v = getDouble(value);
                    List<ContentAndPosition> textPositionList = getContentAndPositionList2(kvText);
                    if(StringUtils.isEmpty(value)){
                        continue;
                    }
                    IaadIndicatorGradeDetail iaadIndicatorGradeDetail = getIaadIndicatorGradeDetail(indicator,textPositionList,doubleToBigDecimal(v),pageNum,value,String.format("房地产新开工面积%s",value));
                    if(iaadIndicatorGradeDetail!=null) list.add(iaadIndicatorGradeDetail);
                }
            }
            return list;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //资产规模
    /**
     3、资产规模 --》ZCGM
     --》M-6、ZCFZ.ZCZJ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、缺失指标分类？"
     */
    private IaadIndicatorGradeDetail getZCGM(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("ZCGM");
            String ZCZJS = null;
            double ZCZJ1 = 0;
            String unit = "";
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> col1 = model.getTable(getJYN(model.getTables()));
            if(MapUtils.isNotEmpty(col1)){
                if(col1.get("ZCZJ")!=null && col1.get("ZCZJ").getValue()!=null){
                    ZCZJS = col1.get("ZCZJ").getValue().getText();
                    ZCZJ1 = getDouble(ZCZJS);
                }
            }
            if(StringUtils.isEmpty(ZCZJS)){
                return null;
            }
            unit=model.getUnitType();
            ZCZJS=ZCZJS+unit;
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(ZCZJ1),unit,pageNum,String.format("近一年资产规模%s",ZCZJS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //产能利用率
    /**
     4、产能利用率 --》CNLYL
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、缺失指标分类？"
     单位：%
     */
    private List<IaadIndicatorGradeDetail> getCNLYL(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("CNLLY");
            List<IaadIndicatorGradeDetail> list = new ArrayList<>();
            int pageNum = getChapterModel("M-5").getStartPage();
            List<KVText> kvTextList = getKVTextValues("M-5","SN","CNLYL");
            if(CollectionUtils.isNotEmpty(kvTextList)){
                for(KVText kvText : kvTextList){
                    String value = kvText.getText();
                    double v = getDouble(value);
                    List<ContentAndPosition> textPositionList = getContentAndPositionList2(kvText);
                    if(StringUtils.isEmpty(value)){
                        continue;
                    }
                    IaadIndicatorGradeDetail iaadIndicatorGradeDetail = getIaadIndicatorGradeDetail(indicator,textPositionList,doubleToBigDecimal(v),"",pageNum,String.format("产能利用率%s",value));
                    if(iaadIndicatorGradeDetail!=null) list.add(iaadIndicatorGradeDetail);
                }
            }
            return list;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //营业收入增长率
    /**
     5、营业收入增长率 --》（2017年值-2016年值）/2016年值 --》程序计算
     --》M-6、LR.YYSR
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、缺失指标分类,1031确认：抽取2017年末营业收入和2016年末营业收入；公式：（2017年值-2016年值）/2016年值"
     单位：%
     */
    private IaadIndicatorGradeDetail getYYSR(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("YYSRZZL");
            String str1=null,str2=null;
            double v1=0,v2=0,v=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","LR");
            String[] years = getSNJYQ(model.getTables());
            if(years!=null && years.length>=3){
                Map<String, ExtractItem> col1 = model.getTable(years[2]);
                Map<String, ExtractItem> col2 = model.getTable(years[1]);
                if(MapUtils.isNotEmpty(col1) && col1.get("YYSR")!=null && col1.get("YYSR").getValue()!=null){
                    str1 = col1.get("YYSR").getValue().getText();
                    v1 = getDouble(str1);
                }
                if(MapUtils.isNotEmpty(col2) && col2.get("YYSR")!=null && col2.get("YYSR").getValue()!=null){
                    str2 = col2.get("YYSR").getValue().getText();
                    v2 = getDouble(str2);
                }
            }
            if(v1!=0){
                v = getTwoDecimal((v2-v1)/v1);
            }
            if(StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2)){
                return null;
            }
            str1=str1+model.getUnitType();
            str2=str2+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"%",pageNum,String.format("%s营业收入%s，%s营业收入%s",years[2],str1,years[1],str2));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //EBITDA总资产报酬率
    /**
     6、EBITDA总资产报酬率 --》EBITDA/总资产*100%
     --》M-6、CZNL.EBITDA
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、M-6会计科目分析获取；
     3、确认获取近一年"
     单位：%
     */
    private IaadIndicatorGradeDetail getEBITDA(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("EBITDAZZCBCL");
            String EBITDAS = null,ZCZJS=null;
            double EBITDA = 0,ZCZJ=0,v=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","CZNL");
            Map<String, ExtractItem> CZNLcol = model.getTable(getJYN(model.getTables()));
            if(MapUtils.isNotEmpty(CZNLcol) && CZNLcol.get("EBITDA")!=null && CZNLcol.get("EBITDA").getValue()!=null){
                EBITDAS = CZNLcol.get("EBITDA").getValue().getText();
                EBITDA = getDouble(EBITDAS);
            }
            ExtractBizModel model2 = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> col = model2.getTable(getJYN(model2.getTables()));
            if(MapUtils.isNotEmpty(col) && col.get("ZCZJ")!=null && col.get("ZCZJ").getValue()!=null){
                ZCZJS = col.get("ZCZJ").getValue().getText();
                ZCZJ = getDouble(ZCZJS);
            }
            if(EBITDA!=0 && ZCZJ!=0){
                v = getTwoDecimal((EBITDA/ZCZJ));
            }
            if(StringUtils.isEmpty(EBITDAS) && StringUtils.isEmpty(ZCZJS)){
                return null;
            }
            EBITDAS=EBITDAS+model.getUnitType();
            ZCZJS=ZCZJS+model2.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"%",pageNum,String.format("近一年EBITDA%s，近一年总资产%s",EBITDAS,ZCZJS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //是否出具非标准审计意见
    /**
     7、是否出具非标准审计意见 --》SJYJ
     --》M-6、关键字
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、缺失指标分类？"
     */
    private IaadIndicatorGradeDetail getSJYJ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("SFCJFBZSJYJ");
            Map<String,String> keywords = new HashMap<>();
            keywords.put("标准无保留","5");
            keywords.put("带强调段的无保留","20");
            keywords.put("保留意见","100");
            Class clazz = Class.forName("cn.com.cfae.iras.doc.analyze.components.integrity.I1001");
            I1001 i1001 = (I1001) ApplicationContextHelper.getBean("I1001");
            Method setExtractData = clazz.getMethod("setExtractData", DocumentModel.class);
            setExtractData.invoke(i1001, getExtractData());
            Method ifKeywordExist2 = clazz.getMethod("ifKeywordExist2", String.class);
            IaadIndicatorGradeDetail detail = sjyj(i1001,"带强调段的无保留",ifKeywordExist2,keywords,indicator,"20");
            if(detail!=null){
                return detail;
            }else{
                IaadIndicatorGradeDetail detail1 = sjyj(i1001,"标准无保留",ifKeywordExist2,keywords,indicator,"5");
                if(detail1!=null){
                    return detail1;
                }else{
                    return sjyj(i1001,"保留意见",ifKeywordExist2,keywords,indicator,"100");
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    private IaadIndicatorGradeDetail sjyj(I1001 i1001,String key,Method ifKeywordExist2,Map<String,String> keywords,IndustryIndicatorList indicator,String thresholdScore) throws Exception{
        i1001.setKeyword(key);
        Map map = (Map)ifKeywordExist2.invoke(i1001, "M-6");
        if(MapUtils.isNotEmpty(map)){
            boolean boo = (boolean)map.get("exist");
            if(boo){
                double score = Double.valueOf(keywords.get(key))*Double.valueOf(indicator.getWeight());
                return getIaadIndicatorGradeDetail(indicator,key, getTwoDecimal(score)+"", key,thresholdScore, (int)map.get("pageNum"), key);
            }
        }
        return null;
    }

    //毛利率
    /**
     8、毛利率 --》MLL --》（营业收入-营业成本）/营业收入 --》(YYSR-YYCB)/YYSR --》(目前采用抽取值，不适用公式计算)
     --》M-5、MLL.MLL 或 LR.YYSR、LR.YYCB
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供测算公式；1031确认毛利率=（营业收入-营业成本）/营业收入
     1102一期按现在的方式找不到就人工手输入结果，二期界面调整，增加公式计算功能
     3、获取近一年年报基础数据后进行测算；"
     单位：%
     */
    private IaadIndicatorGradeDetail getMLL(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("MLL");
            ExtractBizModel model = getModel("M-6","LR");
            Map<String, ExtractItem> JLRcol = model.getTable(getJYN(model.getTables()));
            String YYSRS = null,YYCBS = null;
            double YYSR = 0,YYCB = 0,v = 0;
            int pageNum = getChapterModel("M-6").getStartPage();
            if(MapUtils.isNotEmpty(JLRcol) && JLRcol.get("YYSR")!=null && JLRcol.get("YYSR").getValue()!=null){
                YYSRS = JLRcol.get("YYSR").getValue().getText();
                YYSR = getDouble(YYSRS);
            }
            if(MapUtils.isNotEmpty(JLRcol) && JLRcol.get("YYCB")!=null && JLRcol.get("YYCB").getValue()!=null){
                YYCBS = JLRcol.get("YYCB").getValue().getText();
                YYCB = getDouble(YYCBS);
            }
            if(YYSR!=0){
                v = getTwoDecimal((YYSR-YYCB)/YYSR);
            }
            if(StringUtils.isEmpty(YYSRS) && StringUtils.isEmpty(YYCBS)){
                return null;
            }
            YYSRS=YYSRS+model.getUnitType();
            YYCBS=YYCBS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"%",pageNum,String.format("近一年营业收入%s,近一年营业成本%s",YYSRS,YYCBS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
//        直接抽取
//        String MLLS = null;
//        double MLLD = 0;
//        boolean boo = false;
//        int pageNum = getChapterModel("M-5").getStartPage();
//        List<Map<String, ExtractItem>> mll = getModel("M-5","MLL").getTable2(getJYN());
//        String[] arr = new String[]{"水泥","混凝土"};
//        for(int i=0;i<arr.length;i++){
//            if(boo) break;
//            for(Map<String, ExtractItem> item : mll){
//                if(boo) break;
//                if(item.get("YWBK")!=null && item.get("YWBK").getValue()!=null){
//                    if(StringUtils.isNotEmpty(item.get("YWBK").getValue().getText()) && item.get("YWBK").getValue().getText().contains(arr[i])){
//                        if(item.get("ZB")!=null && item.get("ZB").getValue()!=null){
//                            MLLS = item.get("ZB").getValue().getText();
//                            if(MLLS.contains("%")){
//                                MLLD = (getDouble(MLLS)/100);
//                            }else{
//                                MLLD = (getDouble(MLLS));
//                            }
//                            pageNum = item.get("ZB").getValue().getPageNumber();
//                        }
//                        boo = true;
//                    }
//                }
//            }
//        }
//        if(StringUtils.isEmpty(MLLS)){
//            return null;
//        }
//        return getIaadIndicatorGradeDetail(indicator,MLLD+"",pageNum,String.format("毛利率%s",MLLS));
//        使用公式计算（营业收入-营业成本）/营业收入
    }

    //净利率
    /**
     9、净利率 --》净利润/营业收入 --》JLR/YYSR
     --》M-6、LR.JLR、LR.YYSR
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供测算公式；1031确认净利率=净利润/营业收入
     1102一期按现在的方式找不到就人工手输入结果，二期界面调整，增加公式计算功能
     3、获取近一年年报基础数据后进行测算；"
     单位：%
     */
    private IaadIndicatorGradeDetail getJLR_YYSR(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("JLL");
            ExtractBizModel model = getModel("M-6","LR");
            Map<String, ExtractItem> JLRcol = model.getTable(getJYN(model.getTables()));
            String JLRS = null,YYWSRS = null;
            double JLR = 0,YYWSR = 0,v = 0;
            int pageNum = getChapterModel("M-6").getStartPage();
            if(MapUtils.isNotEmpty(JLRcol) && JLRcol.get("JLR")!=null && JLRcol.get("JLR").getValue()!=null){
                JLRS = JLRcol.get("JLR").getValue().getText();
                JLR = getDouble(JLRS);
            }
            if(MapUtils.isNotEmpty(JLRcol) && JLRcol.get("YYWSR")!=null && JLRcol.get("YYWSR").getValue()!=null){
                YYWSRS = JLRcol.get("YYWSR").getValue().getText();
                YYWSR = getDouble(YYWSRS);
            }
            if(JLR!=0 && YYWSR!=0){
                v = getTwoDecimal(JLR/YYWSR);
            }
            if(StringUtils.isEmpty(JLRS) && StringUtils.isEmpty(YYWSRS)){
                return null;
            }
            JLRS=JLRS+model.getUnitType();
            YYWSRS=YYWSRS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"%",pageNum,String.format("近一年净利润%s,近一年营业收入%s",JLRS,YYWSRS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //资产负债率
    /**
     10、资产负债率 --》负债总额（负债总计、总负债）/资产总额（资产总计、总资产）--》ZFZ/ZZC
     --》M-6、ZCFZ.FZHJ、ZCFZ.ZCZJ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供测算公式；1031确认资产负债率=负债总额（负债总计、总负债）/资产总额（资产总计、总资产）
     1102一期按现在的方式找不到就人工手输入结果，二期界面调整，增加公式计算功能
     3、获取近一年年报基础数据后进行测算；"
     单位：%
     */
    private IaadIndicatorGradeDetail getZFZ_ZZC(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("ZCFZL");
            String FZHJS =null,ZCZJS = null;
            double FZHJ1 =0,ZCZJ1 = 0,v1=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> col1 = model.getTable(getJYN(model.getTables()));
            if(MapUtils.isNotEmpty(col1)){
                if(col1.get("FZHJ")!=null && col1.get("FZHJ").getValue()!=null){
                    FZHJS = col1.get("FZHJ").getValue().getText();
                    FZHJ1 = getDouble(FZHJS);
                }
                if(col1.get("ZCZJ")!=null && col1.get("ZCZJ").getValue()!=null){
                    ZCZJS = col1.get("ZCZJ").getValue().getText();
                    ZCZJ1 = getDouble(ZCZJS);
                }
                if(FZHJ1!=0 && ZCZJ1!=0){
                    v1 = getTwoDecimal(FZHJ1/ZCZJ1);
                }
            }
            if(StringUtils.isEmpty(FZHJS) && StringUtils.isEmpty(ZCZJS)){
                return null;
            }
            FZHJS=FZHJS+model.getUnitType();
            ZCZJS=ZCZJS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v1),"%",pageNum,String.format("近一年负债总额%s，近一年资产总额%s",FZHJS,ZCZJS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //流动负债/负债合计
    /**
     11、流动负债/负债合计 --》LDFZ/ZFZ
     --》M-6、ZCFZ.LDFZHJ、ZCFZ.FZHJ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供测算公式；
     3、获取近一年年报基础数据后进行测算；"
     单位：%
     */
    private IaadIndicatorGradeDetail getLDFZ_ZFZ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("LDFZ-FZHJ");
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> ZCFZcol = model.getTable(getJYN(model.getTables()));
            String LDFZHJS=null,FZHJS=null;
            double LDFZHJ=0,FZHJ=0,v=0;
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("LDFZHJ")!=null && ZCFZcol.get("LDFZHJ").getValue()!=null){
                LDFZHJS = ZCFZcol.get("LDFZHJ").getValue().getText();
                LDFZHJ = getDouble(LDFZHJS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("FZHJ")!=null && ZCFZcol.get("FZHJ").getValue()!=null){
                FZHJS = ZCFZcol.get("FZHJ").getValue().getText();
                FZHJ = getDouble(FZHJS);
            }
            if(LDFZHJ!=0 && FZHJ!=0){
                v = getTwoDecimal(LDFZHJ/FZHJ);
            }
            if(StringUtils.isEmpty(LDFZHJS) && StringUtils.isEmpty(FZHJS)){
                return null;
            }
            LDFZHJS=LDFZHJS+model.getUnitType();
            FZHJS=FZHJS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"%",pageNum,String.format("近一年流动负债%s，近一年负债合计%s",LDFZHJS,FZHJS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //经营性净现金流净额
    /**
     12、经营性净现金流净额 --》XJL
     --》M-6、XJLL.JYHDCSXJLLJE
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、近三年年报；"
     */
    private IaadIndicatorGradeDetail getXJL(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("JYXXJLJE");
            String thresholdValue=null,automateScore=null,thresholdScore=null;
            String s1=null,s2=null,s3=null;
            double v1=0,v2=0,v3=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","XJLL");
            String[] years = getSNJYQ(model.getTables());
            if(years!=null && years.length>=3){
                Map<String, ExtractItem> col1 = model.getTable(years[1]);
                Map<String, ExtractItem> col2 = model.getTable(years[2]);
                Map<String, ExtractItem> col3 = model.getTable(years[0]);
                if(col1!=null && col2!=null && col3!=null){
                    KVText value1 = col1.get("JYHDCSXJLLJE")!=null?col1.get("JYHDCSXJLLJE").getValue():null;
                    KVText value2 = col2.get("JYHDCSXJLLJE")!=null?col2.get("JYHDCSXJLLJE").getValue():null;
                    KVText value3 = col3.get("JYHDCSXJLLJE")!=null?col3.get("JYHDCSXJLLJE").getValue():null;
                    if(value1!=null && value2!=null && value3!=null){
                        s1 = value1.getText();
                        v1 = getDouble(s1);
                        s2 = value2.getText();
                        v2 = getDouble(s2);
                        s3 = value3.getText();
                        v3 = getDouble(s3);
                        Map map = getXJLScoreAndThresholdValue(v1,v2,v3,indicator.getWeight(),indicator.getThresholdList());
                        thresholdValue = (String) map.get("thresholdValue");
                        automateScore = (String) map.get("automateScore");
                        thresholdScore = (String) map.get("thresholdScore");
                    }
                }
            }
            if(StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2) && StringUtils.isEmpty(s3)){
                return null;
            }
            s1=s1+model.getUnitType();
            s2=s2+model.getUnitType();
            s3=s3+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v1)+";"+doubleToBigDecimal(v2)+";"+doubleToBigDecimal(v3),automateScore,thresholdValue,thresholdScore,pageNum,String.format("近两年及一期经营性现金流%s,%s,%s",s1,s2,s3));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //    经营性现金流量净额分数及阈值计算
    private Map getXJLScoreAndThresholdValue(double v1,double v2,double v3,String weight,List<IndustryIndicatorThreshold> industryIndicatorThresholdList){
        Map rs = new HashMap();
        double automateScore = 0;//得分 = 阈值对应分数 * 权重
        int score = 0,thresholdScore=0;
        String thresholdValue = null;
        if(v1>0 && v2>0 && v3>0){
            thresholdScore = score = 0;
            thresholdValue = "连续3年为正";
        }else if((v1>0 && v2>0) || (v2>0 && v3>0)){
            thresholdScore = score = 20;
            thresholdValue = "连续2年为正";
        }else if(v3>0){
            thresholdScore = score = 50;
            thresholdValue = "本年度为正";
        }else if(v3<0){
            thresholdScore = score = 80;
            thresholdValue = "本年度为负";
        }else if((v1<0 && v2<0) || (v2<0 && v3<0)){
            thresholdScore = score = 100;
            thresholdValue = "连续2年为负";
        }else{
            thresholdValue="未匹配到阈值";
        }
        automateScore = score*Double.valueOf(weight);
        rs.put("automateScore",automateScore+"");
        rs.put("thresholdValue",thresholdValue);
        rs.put("thresholdScore",thresholdScore==0?null:thresholdScore+"");
        return rs;
    }

    //流动比率（短期偿债指标）
    /**
     13、流动比率（短期偿债指标） --》LDBL --》流动资产/流动负债  --》LDZC/LDFZ
     --》M-6、CZNL.LDBL 或 ZCFZ.LDZCHJ、ZCFZ.LDFZHJ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、M-6会计科目分析获取；
     3、确认获取近一年"
     */
    private IaadIndicatorGradeDetail getLDZC_LDFZ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("LDBL");
    //        直接抽取
            ExtractBizModel model = getModel("M-6","CZNL");
            Map<String, ExtractItem> CZNLcol = model.getTable(getJYN(model.getTables()));
            String LDBLS=null;
            double LDBL = 0;
            int pageNum = getChapterModel("M-6").getStartPage();
            if(MapUtils.isNotEmpty(CZNLcol) && CZNLcol.get("LDBL")!=null && CZNLcol.get("LDBL").getValue()!=null){
                LDBLS = CZNLcol.get("LDBL").getValue().getText();
                LDBL = (getDouble(LDBLS));
                pageNum = CZNLcol.get("LDBL").getValue().getPageNumber();
            }
            if(StringUtils.isEmpty(LDBLS)){
                return null;
            }
            if(!LDBLS.contains("%")) LDBLS=LDBLS+"%";
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(LDBL),"",pageNum,String.format("近一年流动比率%s",LDBLS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
//        公式计算
//        String LDFZHJS=null,LDZCHJS=null;
//        double v=0,LDFZHJ=0,LDZCHJ=0;
//        Map<String, ExtractItem> ZCFZcol = getModel("M-6","ZCFZ").getTable(getJYN());
//        if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("LDFZHJ")!=null && ZCFZcol.get("LDFZHJ").getValue()!=null){
//            LDFZHJS = ZCFZcol.get("LDFZHJ").getValue().getText();
//            LDFZHJ = getDouble(LDFZHJS);
//        }
//        if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("LDZCHJ")!=null && ZCFZcol.get("LDZCHJ").getValue()!=null){
//            LDZCHJS = ZCFZcol.get("LDZCHJ").getValue().getText();
//            LDZCHJ = getDouble(LDZCHJS);
//        }
//        if(LDFZHJ!=0 && LDZCHJ!=0){
//            v = (LDZCHJ/LDFZHJ);
//        }
//        if(StringUtils.isEmpty(LDFZHJS) && StringUtils.isEmpty(LDZCHJS)){
//            return null;
//        }
//        return getIaadIndicatorGradeDetail(indicator,v+"",0,String.format("近一年流动资产%s，近一年流动负债%s",LDZCHJS,LDFZHJS));
    }

    //全部债务/EBITDA（长期偿债指标）
    /**
     14、全部债务/EBITDA（长期偿债指标）--》（短期借款+长期借款+一年内到期非流动负债+应付债券+应付短期债券（如有）+应付长期债券（如有）+其他非流动负债）/EBITDA --》(DQJK+CQJK+FLDFZ+YFZQ+YFDQZQ+YFCQZQ+QTFLDFZ)/EBITDA
     --》M-6、ZCFZ.DQJK、ZCFZ.CQJK、ZCFZ.YNNDQFLDFZ、ZCFZ.YFZQ、ZCFZ.YFDQZQ、ZCFZ.YFCQZQ、ZCFZ.QTFLDFZ、CZNL.EBITDA
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供全部债务对应科目；
     3、提供测算公式；1031确认全部债务/EBITDA= （短期借款+长期借款+一年内到期非流动负债+应付债券+应付短期债券（如有）+应付长期债券（如有）+其他非流动负债）/EBITDA
     4、确认近一年
     5、EBITDA从M-6会计科目分析章节获取；"
     */
    private IaadIndicatorGradeDetail getALL_EBITDA(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("QBZW-BEITDA");
            String EBITDAS=null,DQJKS=null,CQJKS=null,YNNDQFLDFZS=null,YFZQS=null,YFDQZQS=null,YFCQZQS=null,QTFLDFZS=null;
            double EBITDA=0,DQJK=0,CQJK=0,YNNDQFLDFZ=0,YFZQ=0,YFDQZQ=0,YFCQZQ=0,QTFLDFZ=0,ALL=0,v=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel CZNLmodel = getModel("M-6","CZNL");
            Map<String, ExtractItem> CZNLcol = CZNLmodel.getTable(getJYN(CZNLmodel.getTables()));
            if(MapUtils.isNotEmpty(CZNLcol) && CZNLcol.get("EBITDA")!=null && CZNLcol.get("EBITDA").getValue()!=null){
                EBITDAS = CZNLcol.get("EBITDA").getValue().getText();
                EBITDA = getDouble(EBITDAS);
            }
            ExtractBizModel ZCFZmodel = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> ZCFZcol = ZCFZmodel.getTable(getJYN(ZCFZmodel.getTables()));
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("DQJK")!=null && ZCFZcol.get("DQJK").getValue()!=null){
                DQJKS = ZCFZcol.get("DQJK").getValue().getText();
                DQJK = getDouble(DQJKS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("CQJK")!=null && ZCFZcol.get("CQJK").getValue()!=null){
                CQJKS = ZCFZcol.get("CQJK").getValue().getText();
                CQJK = getDouble(CQJKS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("YNNDQFLDFZ")!=null && ZCFZcol.get("YNNDQFLDFZ").getValue()!=null){
                YNNDQFLDFZS = ZCFZcol.get("YNNDQFLDFZ").getValue().getText();
                YNNDQFLDFZ = getDouble(YNNDQFLDFZS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("YFZQ")!=null && ZCFZcol.get("YFZQ").getValue()!=null){
                YFZQS = ZCFZcol.get("YFZQ").getValue().getText();
                YFZQ = getDouble(YFZQS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("YFDQZQ")!=null && ZCFZcol.get("YFDQZQ").getValue()!=null){
                YFDQZQS = ZCFZcol.get("YFDQZQ").getValue().getText();
                YFDQZQ = getDouble(YFDQZQS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("YFCQZQ")!=null && ZCFZcol.get("YFCQZQ").getValue()!=null){
                YFCQZQS = ZCFZcol.get("YFCQZQ").getValue().getText();
                YFCQZQ = getDouble(YFCQZQS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("QTFLDFZ")!=null && ZCFZcol.get("QTFLDFZ").getValue()!=null){
                QTFLDFZS = ZCFZcol.get("QTFLDFZ").getValue().getText();
                QTFLDFZ = getDouble(QTFLDFZS);
            }
            ALL = DQJK+CQJK+YNNDQFLDFZ+YFZQ+YFDQZQ+YFCQZQ+QTFLDFZ;
            if(EBITDA!=0){
                v = getTwoDecimal(ALL/EBITDA);
            }
            if(StringUtils.isEmpty(EBITDAS) || (StringUtils.isEmpty(EBITDAS) && StringUtils.isEmpty(DQJKS) && StringUtils.isEmpty(CQJKS) && StringUtils.isEmpty(YNNDQFLDFZS) && StringUtils.isEmpty(YFZQS) &&
                    StringUtils.isEmpty(YFDQZQS) && StringUtils.isEmpty(YFCQZQS) && StringUtils.isEmpty(QTFLDFZS))){
                return null;
            }
            EBITDAS=EBITDAS+CZNLmodel.getUnitType();
            DQJKS=DQJKS+ZCFZmodel.getUnitType();
            CQJKS=CQJKS+ZCFZmodel.getUnitType();
            YNNDQFLDFZS=YNNDQFLDFZS+ZCFZmodel.getUnitType();
            YFZQS=YFZQS+ZCFZmodel.getUnitType();
            YFDQZQS=YFDQZQS+ZCFZmodel.getUnitType();
            YFCQZQS=YFCQZQS+ZCFZmodel.getUnitType();
            QTFLDFZS=QTFLDFZS+ZCFZmodel.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"",pageNum, String.format("近一年EBITDA%s，短期借款%s，长期借款%s，一年内到期非流动负债%s，应付债券%s，应付短期债券%s，应付长期债券%s，其他非流动负债%s", EBITDAS,DQJKS,CQJKS,YNNDQFLDFZS,YFZQS,YFDQZQS,YFCQZQS,QTFLDFZS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //销售商品、提供劳务收到的现金/短期负债
    /**
     15、销售商品、提供劳务收到的现金/短期负债 --》销售商品、提供劳务收到的现金（现金流量）/（短期借款+一年内到期的非流动负债）--》SDXJ/(DQJK+FLDFZ)
     --》M-6、XJLL.JYHDCSXJLLJE、ZCFZ.DQJK、ZCFZ.YNNDQFLDFZ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供测算公式；1031确认销售商品、提供劳务收到的现金/短期负债=销售商品、提供劳务收到的现金/（短期借款+一年内到期的非流动负债）
     3、确认近一年"
     */
    private IaadIndicatorGradeDetail getSDXJ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("XJ-DQFZ");
            String DQJKS=null,YNNDQFLDFZS=null,JYHDCSXJLLS=null;
            double DQJK=0,YNNDQFLDFZ=0,JYHDCSXJLL=0,v=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> ZCFZcol = model.getTable(getJYN(model.getTables()));
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("DQJK")!=null && ZCFZcol.get("DQJK").getValue()!=null){
                DQJKS = ZCFZcol.get("DQJK").getValue().getText();
                DQJK = getDouble(DQJKS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("YNNDQFLDFZ")!=null && ZCFZcol.get("YNNDQFLDFZ").getValue()!=null){
                YNNDQFLDFZS = ZCFZcol.get("YNNDQFLDFZ").getValue().getText();
                YNNDQFLDFZ = getDouble(YNNDQFLDFZS);
            }
            ExtractBizModel XJLLmodel = getModel("M-6","XJLL");
            Map<String, ExtractItem> XJLLcol = XJLLmodel.getTable(getJYN(XJLLmodel.getTables()));
            if(MapUtils.isNotEmpty(XJLLcol) && XJLLcol.get("JYHDCSXJLLJE")!=null && XJLLcol.get("JYHDCSXJLLJE").getValue()!=null){
                JYHDCSXJLLS = XJLLcol.get("JYHDCSXJLLJE").getValue().getText();
                JYHDCSXJLL = getDouble(JYHDCSXJLLS);
            }
            if(YNNDQFLDFZ+DQJK!=0){
                v = getTwoDecimal((JYHDCSXJLL/(YNNDQFLDFZ+DQJK)));
            }
            if(StringUtils.isEmpty(DQJKS) && StringUtils.isEmpty(YNNDQFLDFZS) && StringUtils.isEmpty(JYHDCSXJLLS)){
                return null;
            }
            JYHDCSXJLLS=JYHDCSXJLLS+XJLLmodel.getUnitType();
            DQJKS=DQJKS+model.getUnitType();
            YNNDQFLDFZS=YNNDQFLDFZS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"",pageNum,String.format("近一年现金流量%s，近一年短期借款%s，近一年一年内到期的非流动负债%s",JYHDCSXJLLS,DQJKS,YNNDQFLDFZS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //货币资金/短期债务
    /**
     16、货币资金/短期债务 --》货币资金/（短期借款+一年内到期的非流动负债）--》HBZJ/(DQJK+FLDFZ)
     --》M-6、ZCFZ.HBZJ、ZCFZ.DQJK、ZCFZ.YNNDQFLDFZ
     "1、需求讨论去除小计，重新调整单项指标权重；
     2、提供测算公式:货币资金/（短期借款+一年内到期的非流动负债）
     3、确认近一年"
     */
    private IaadIndicatorGradeDetail getHBZJ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("HBZJ-DQFZ");
            String DQJKS=null,YNNDQFLDFZS=null,HBZJS=null;
            double DQJK=0,YNNDQFLDFZ=0,HBZJ=0,v=0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> ZCFZcol = model.getTable(getJYN(model.getTables()));
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("DQJK")!=null && ZCFZcol.get("DQJK").getValue()!=null){
                DQJKS = ZCFZcol.get("DQJK").getValue().getText();
                DQJK = getDouble(DQJKS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("YNNDQFLDFZ")!=null && ZCFZcol.get("YNNDQFLDFZ").getValue()!=null){
                YNNDQFLDFZS = ZCFZcol.get("YNNDQFLDFZ").getValue().getText();
                YNNDQFLDFZ = getDouble(YNNDQFLDFZS);
            }
            if(MapUtils.isNotEmpty(ZCFZcol) && ZCFZcol.get("HBZJ")!=null && ZCFZcol.get("HBZJ").getValue()!=null){
                HBZJS = ZCFZcol.get("HBZJ").getValue().getText();
                HBZJ = getDouble(HBZJS);
            }
            if(YNNDQFLDFZ+DQJK!=0){
                v = getTwoDecimal(HBZJ/(YNNDQFLDFZ+DQJK));
            }
            if(StringUtils.isEmpty(DQJKS) && StringUtils.isEmpty(YNNDQFLDFZS) && StringUtils.isEmpty(HBZJS)){
                return null;
            }
            HBZJS=HBZJS+model.getUnitType();
            DQJKS=DQJKS+model.getUnitType();
            YNNDQFLDFZS=YNNDQFLDFZS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"",pageNum,String.format("近一年货币资金%s，近一年短期借款%s，近一年一年内到期的非流动负债%s",HBZJS,DQJKS,YNNDQFLDFZS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }



    @Override
    public List<ComponentResult> handlerM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }
}
